package cn.kailangtu.dataspot.tasks;

import android.os.SystemClock;
import android.util.Log;

import cn.kailangtu.commontools.AppVariable;
import cn.kailangtu.commontools.CommonUtils;
import cn.kailangtu.commontools.Loggers;
import cn.kailangtu.dataspot.R;
import cn.kailangtu.dataspot.data.HighData;
import cn.kailangtu.dataspot.status;
import cn.kailangtu.modbus.ModbusSlave;
import cn.kailangtu.serialutils.api.Com1;
import cn.kailangtu.serialutils.api.ReceiveMsgListenerV2;
import cn.kailangtu.serialutils.api.SerialPortApi;
import cn.kailangtu.serialutils.modbus.modbusUtil;

public class PLCTask extends BaseTask{

    @Override
    void init() {

        initCommand();
        initReceive();
        initSend();
    }

    @Override
    void close() {
        isRun = false;
        Com1.getInstance().close();
    }

    private void initCommand(){
        byte[] cmd1 = modbusUtil.read(1, new byte[]{0x0D, (byte) 0xAB}, new byte[]{0x00, 0x21});
        byte[] cmd2 = modbusUtil.read(1, new byte[]{0x11, (byte) 0x94}, new byte[]{0x00, 0x50});
        lsReadCom.add(cmd1);
        lsReadCom.add(cmd2);

    }



    private void initReceive()
    {
        if(AppVariable.platform_version == 450)
        {
            Com1.getInstance().openSerialPort485(new ReceiveMsgListenerV2() {
                @Override
                public void onReceiveData(byte[] data) {
                  //  Loggers.errInfo("COM1来了：");
                    receiveTime = System.currentTimeMillis();
                    Log.e("DiffTime,COM1", (receiveTime - sendTime) + "ms");
                    Log.d("SHOOTLEE_PLC","收到数据，按理是不等了");
                   // Log.d("SEND AND RECEIVE","RECEIVE:PROCESS,BEGIN");
                    HighData.getInstance().readNewData(data);
                    Log.d("SHOOTLEE_PLC","收到数据，按理完了");
                   // Log.d("SEND AND RECEIVE","RECEIVE:PROCESS,END");
                    isWaitReadCom = false;
                }
            });
        }
        else {
            Com1.getInstance().openSerialPort("dev/ttyS1", 38400, 0, new ReceiveMsgListenerV2() {
                @Override
                public void onReceiveData(byte[] data) {
                    Loggers.errInfo("COM1来了：");
                    receiveTime = System.currentTimeMillis();
                    Log.e("DiffTime,COM1", (receiveTime - sendTime) + "ms");
                    HighData.getInstance().readNewData(data);
                    SystemClock.sleep(10);
                    isWaitReadCom = false;
                }
            });
        }
    }

    private void initSend()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(1000);
                Log.d("SHOOTLEE_WATCHDOG","打开PLC发送器");
                while (isRun) {
                    if (SerialPortApi.getInstance().isOpen()) {
                        SystemClock.sleep(5);
                        if (!isWaitReadCom) {
                            isWaitReadCom = true;
                            isWaitReadIndexCom = 0;
                            if (plcQuere.size() > 0) {
                                byte[] data = plcQuere.poll();
                                sendTime = System.currentTimeMillis();
                                Log.d("SHOOTLEE_PLC","发送："+ CommonUtils.ByteArrayToHexString(data));
                                Com1.getInstance().sendData(data);
                                Log.d("SHOOTLEE_PLC","发送完等");

                            } else {
                                sendTime = System.currentTimeMillis();
                                Log.d("SHOOTLEE_PLC","发送："+ CommonUtils.ByteArrayToHexString(lsReadCom.get(indexCom)));
                                Com1.getInstance().sendData(lsReadCom.get(indexCom));
                                Log.d("SHOOTLEE_PLC","发送完等");

                                indexCom++;
                                if (indexCom >= lsReadCom.size()) {
                                    indexCom = 0;
                                }

                            }

                        } else {
                            isWaitReadIndexCom++;
                            Log.d("SHOOTLEE_PLC","发送完等，索引："+isWaitReadIndexCom);
                            if (isWaitReadIndexCom > 200) {
                                isWaitReadCom = false;
                                Log.e("NEWNEWNEW","COM1超时："+(System.currentTimeMillis()-sendTime));
                            }
                        }
                    } else {
                        Log.d("COMCOM", "串口不通。");
                        SystemClock.sleep(1000);
                    }
                }
            }
        }).start();
    }
}
